#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import yaml

from StringIO import StringIO

class FileMetaWriterError(Exception):
    def __init__(self, message):
        Exception.__init__(self, message)

class MetaWriter:
    def write (self, data, file):
        stream = StringIO()

        yaml.dump(data, stream, default_flow_style=False)
        list_len = len(stream.buflist)
        list = stream.buflist[2: list_len] # delete yaml tag & new line terminator

        file.writelines(list)
        file.flush()

class FileMetaWriter:
    def __init__(self):
        self.__file_writer = MetaWriter()

    def write(self, data, file_name="META"):
        fd = None
        try:
            fd = file(file_name, "w")
            self.__file_writer.write(data, fd)
        except Exception, error:
            raise FileMetaWriterError(error.message)
        finally:
            if fd: fd.close()